/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/idenger">idenger</a> All rights reserved.
 */
package com.idenger.modules.edu.web.smsrecord;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.idenger.common.config.Global;
import com.idenger.common.persistence.Page;
import com.idenger.common.web.BaseController;
import com.idenger.common.utils.StringUtils;
import com.idenger.modules.edu.entity.smsrecord.EduSmsRecord;
import com.idenger.modules.edu.service.smsrecord.EduSmsRecordService;

/**
 * 短信记录Controller
 * @author 兰正军
 * @version 2017-12-01
 */
@Controller
@RequestMapping(value = "${adminPath}/edu/smsrecord/eduSmsRecord")
public class EduSmsRecordController extends BaseController {

	@Autowired
	private EduSmsRecordService eduSmsRecordService;
	
	@ModelAttribute
	public EduSmsRecord get(@RequestParam(required=false) String id) {
		EduSmsRecord entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = eduSmsRecordService.get(id);
		}
		if (entity == null){
			entity = new EduSmsRecord();
		}
		return entity;
	}
	
	@RequiresPermissions("edu:smsrecord:eduSmsRecord:view")
	@RequestMapping(value = {"list", ""})
	public String list(EduSmsRecord eduSmsRecord, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<EduSmsRecord> page = eduSmsRecordService.findPage(new Page<EduSmsRecord>(request, response), eduSmsRecord); 
		model.addAttribute("page", page);
		return "modules/edu/smsrecord/eduSmsRecordList";
	}

	@RequiresPermissions("edu:smsrecord:eduSmsRecord:view")
	@RequestMapping(value = "form")
	public String form(EduSmsRecord eduSmsRecord, Model model) {
		model.addAttribute("eduSmsRecord", eduSmsRecord);
		return "modules/edu/smsrecord/eduSmsRecordForm";
	}

	@RequiresPermissions("edu:smsrecord:eduSmsRecord:edit")
	@RequestMapping(value = "save")
	public String save(EduSmsRecord eduSmsRecord, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, eduSmsRecord)){
			return form(eduSmsRecord, model);
		}
		eduSmsRecordService.save(eduSmsRecord);
		addMessage(redirectAttributes, "保存短信记录成功");
		return "redirect:"+Global.getAdminPath()+"/edu/smsrecord/eduSmsRecord/?repage";
	}
	
	@RequiresPermissions("edu:smsrecord:eduSmsRecord:edit")
	@RequestMapping(value = "delete")
	public String delete(EduSmsRecord eduSmsRecord, RedirectAttributes redirectAttributes) {
		eduSmsRecordService.delete(eduSmsRecord);
		addMessage(redirectAttributes, "删除短信记录成功");
		return "redirect:"+Global.getAdminPath()+"/edu/smsrecord/eduSmsRecord/?repage";
	}

}